package com.huawang.mdesign.model.service.util.mindmap;

import com.huawang.mdesign.model.service.constant.mindmap.MindMapEnum;
import com.huawang.mdesign.model.service.util.mindmap.xmind.Root;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 思维导图核心处理类
 *
 * @author admin
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MindMapContext {

    private final ApplicationContext applicationContext;

    /**
     * 解析思维导图
     *
     * @param sourceFile 源文件
     * @param plainText  是否是纯文本
     * @return 树形结构
     */
    @NotNull
    public List<Root> read(@NotNull String sourceFile, boolean plainText) {
        List<Root> res = new ArrayList<>();
        applicationContext.getBeansOfType(MindMap.class).forEach((k, v) ->
                Optional.ofNullable(v)
                        .filter(e -> e.support(plainText ? MindMapEnum.PLAIN_TEXT.getTargetFile() : sourceFile))
                        .map(e -> e.read(sourceFile))
                        .ifPresent(res::addAll)
        );
        return res;
    }

}
